#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include"menu.h"
#include"DynamicArray.h"
#define NameSize 24
#define TelephoneSize 24
#define BookSize 512
#define GrowthFactor 2
struct Contact
{
    char *name;
    char *Telephone;
};
typedef struct Contact Ctact;

Ctact *CreateCtact(const char* name,const char* Telephone)
{
    Ctact *s = (Ctact *)malloc(sizeof(Ctact));
    if(s==NULL)
    {
        printf("用户申请空间失败\n");
        return NULL;
    }
    s->name = strdup(name);
    s->Telephone = strdup(Telephone);
    return s;
}
void PrintCtact(ElementType ptr)
{
    Ctact *s = (Ctact *)ptr;
    printf("学生姓名：%s 电话号码：%s\n",s->name,s->Telephone);
}
void FreeCtact(ElementType ptr)
{
    Ctact *s = (Ctact *)ptr;
    free(s->name);
    free(s->Telephone);
    free(s);
}
bool IsNameEqual(ElementType ptr1,ElementType ptr2)
{
    Ctact *s = (Ctact *)ptr1;
    char *name = (char *)ptr2;
    return strcmp(s->name, name)==0;
}
bool SortByTelephone(ElementType ptr1,ElementType ptr2)
{
    Ctact *s1 = (Ctact *)ptr1;
    Ctact *s2 = (Ctact *)ptr2;
    return s1->Telephone > s2->Telephone;
}
bool SortByName(ElementType ptr1,ElementType ptr2)
{
    Ctact *s1 = (Ctact *)ptr1;
    Ctact *s2 = (Ctact *)ptr2;
    return strcmp(s1->name,s2->name)>0;
}
int SearchByName(ElementType ptr1,void*ptr2)
{
    Ctact *s1 = (Ctact *)ptr1;
    char *name = (char *)ptr2;
    return strcmp(s1->name, name);
}
bool IsPhoneEqual(ElementType ptr1,ElementType ptr2)
{
    Ctact *s = (Ctact *)ptr1;
    char *phone = (char *)ptr2;
    return strcmp(s->Telephone, phone);
}
bool IsNameHave(ElementType ptr1,ElementType ptr2)
{
    Ctact *s = (Ctact *)ptr1;
    char *name = (char *)ptr2;
    if (strstr(s->name,name))
        return true;
    else
        return false;
}
bool IsPhoneHave(ElementType ptr1,ElementType ptr2)
{
    Ctact *s = (Ctact *)ptr1;
    char *phone = (char *)ptr2;
    if (strstr(s->Telephone,phone))
        return true;
    else
        return false;
}

int main()
{
    // FILE *fp;
    // fp = fopen("Contacts.txt", "w"); 
    // if (fp == NULL) {
    //     printf("文件打开失败");
    //     return 1;
    // }
    DArray *a = InitDArray(10, FreeCtact);
    InsertTail(a, CreateCtact("张三", "123456"));
    InsertTail(a, CreateCtact("李四", "12312412"));
    InsertTail(a, CreateCtact("王五", "123124"));
    DArrayFastSort(a,SortByName);
    int expression;
    char name[NameSize] = {0};
    char phone[TelephoneSize] = {0};
    char newname[NameSize] = {0};
    char newphone[TelephoneSize] = {0};
    do
    {
    menu_main();
    printf("请输入您需要的服务序号：");
    scanf("%d", &expression);
    getchar();
    switch (expression)
    {
    case 1:
        DArrayFastSort(a, SortByName);
        PrintArray(a,PrintCtact);
        break;
    case 2:
        printf("请输入姓名：");
        scanf("%s", name);
        PrintCtact(FindByElement(a, name, IsNameHave));
        break;
    case 3:
        printf("请输入电话号码：");
        scanf("%s", phone);
        PrintCtact(FindByElement(a, phone, IsPhoneHave));
        break;
    case 4:
        printf("请输入姓名：");
        scanf("%s", name);
        printf("请输入电话号码：");
        scanf("%s", phone);
        InsertTail(a,CreateCtact(name,phone));
        break;
    case 5:
        printf("请输入姓名：");
        scanf("%s", name);
        RemoveByValue(a,FindByElement(a, name, IsNameEqual));
        break;
    case 6:
        printf("请输入电话号码：");
        scanf("%s", phone);
        RemoveByValue(a,FindByElement(a, phone, IsPhoneEqual));
        break;
    case 7:
        printf("请输入旧的姓名：");
        scanf("%s", name);
        printf("请输入新的姓名：");
        scanf("%s", newname);
        Ctact* temp1 = FindByElement(a, name, IsNameEqual);
        ReviseArrar(a,temp1,CreateCtact(newname,temp1->Telephone));
        break;
    case 8:
        printf("请输入旧的电话号码：");
        scanf("%s", phone);
        printf("请输入新的电话号码：");
        scanf("%s", newphone);
        Ctact* temp2 = FindByElement(a,phone, IsPhoneEqual);
        ReviseArrar(a,temp2,CreateCtact(temp2->name,newphone));
        break;
    case 9:
        printf("感谢您的使用！\n");
        FreeDArray(&a);
        return 0;
    default:
        printf("您输入的序号不对，请重新输入！\n");
        break;
    }
    }while (expression != 9);
    return 0;
}